import {Injectable} from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';

import {UserAuthService} from '@core/services/user-auth.service';
import {InitService} from '@core/services/init.service';
import {ApesMessageService} from '@shared/apes/message';
import {WebsocketService} from '@core/services/websocket/websocket.service';
import {Platform} from '@angular/cdk/platform';
import {ApiService} from '@core/services/http/api.service';
import {UserInfoModel} from "../../domain/user-info-model";
import {AnalysisService} from "../../routes/analysis/analysis.service";

@Injectable()
export class FnCanAuthProvide implements CanActivate {

  constructor(private userAuthService: UserAuthService, private router: Router, private platform: Platform,
              private msg: ApesMessageService, private initService: InitService, private ws: WebsocketService,
              private apiService: ApiService,
              private as:AnalysisService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    console.log("---------------------> 赋能商家后台");
    if (this.userAuthService.isLogin()) {
      this.initService.loadAppData();
      this.as.getLocalPermit(this.userAuthService.getUserInfoObj().userAccount.post.localPermit.id);
      this.ws.connect(this.userAuthService.getUserInfoObj().userAccount.id);
      return true;
    } else {
      this.msg.error('请先登录');
      this.router.navigateByUrl('/empower/login');
      return false;
    }
  }
}
